package com.jd.risk.bankorder.controller.bankorder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jd.risk.bankorder.common.CommonResult;
import com.jd.risk.bankorder.controller.BaseController;
import com.jd.risk.bankorder.entity.bankorder.TeslaBankRequest;
import com.jd.risk.bankorder.service.bankorder.BankOrderReplyService;
import com.jd.risk.bankorder.service.bankorder.BankOrderService;
import com.jd.risk.bankorder.util.CommonUtil;
import com.wangyin.operation.beans.UploadFile;
import com.wangyin.operation.beans.UploadObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bank/bankOrderRequest")
public class BankOrderRequestController extends BaseController{

	@Autowired
	private BankOrderService bankOrderService;

	@Autowired
	private BankOrderReplyService bankOrderReplyService;

	@RequestMapping("/toBankOrderListRequestPage")
	public String toBankOrderList(ModelMap map)
	{
		map.put("bankRequestType","0");
		return "bankorder/bankOrderRequestList";
	}

	@RequestMapping("/toAddBankOrderRequestPage")
	public String toAddBankOrderRequestPage(ModelMap map ){
		map.put("bankRequestType","0");
		return  "bankorder/addBankOrderRequestPage";
	}


	@RequestMapping("toBankOrderDetailRequestPage")
	public String toBankOrderDetailRequestPage(ModelMap map ,  String bankRequestNo){
		List<TeslaBankRequest> requests = bankOrderService.queryByBankRequestNo(bankRequestNo);
		if(CollectionUtils.isNotEmpty(requests))
		{
			TeslaBankRequest request = requests.get(0);
			Long requestId = request.getId();
			if(!request.getBankRequestState().equals("2")){
				map.put("request",toJson(requests.get(0)));
				map.put("bankRequestType",requests.get(0).getBankRequestType());
			}else{
				bankOrderReplyService.getAllDetailForShow(map , requestId);
			}
		}
		return "bankorder/bankOrderDetailRequestPage";
	}

	@RequestMapping("toBankOrderDetailRequestModifyPage")
	public String toBankOrderDetailRequestModifyPage(ModelMap map ,  String bankRequestNo){
		List<TeslaBankRequest> requests = bankOrderService.queryByBankRequestNo(bankRequestNo);
		if(CollectionUtils.isNotEmpty(requests))
		{
			map.put("request",toJson(requests.get(0)));
			map.put("bankRequestType",requests.get(0).getBankRequestType());
		}
		return "bankorder/bankOrderDetailRequestModifyPage";
	}


	@ResponseBody
	@RequestMapping("/bankOrderListRequest")
	public String bankOrderList(TeslaBankRequest request){
		List<TeslaBankRequest> reqList = bankOrderService.queryBankRequest(request);
		Long count = bankOrderService.queryBankRequestCont(request);

		CommonResult<TeslaBankRequest> result = new CommonResult<TeslaBankRequest>();
		result.setSuccess(true);
		result.setRows(reqList);
		result.setTotal(count);
		return toJson(result);
	}

	/**
	 *  保存请求
	 * @param teslaBankRequest
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addBankOrderRequest")
	public  String addBankOrderRequesṭ (TeslaBankRequest teslaBankRequest)
	{
		CommonResult<String> result = bankOrderService.saveRequest(teslaBankRequest);
		return toJson(result);
	}

	/**
	 *  删除请求
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteBankOrderRequest")
	public  String deleteBankOrderRequesṭ (String id)
	{
		Map<String,Object> result = bankOrderService.deleteRequest(id);
		return toJson(result);
	}

	@ResponseBody
	@RequestMapping("/updateBankOrderRequest")
	public String updateBankOrderRequest(TeslaBankRequest teslaBankRequest)
	{
		CommonResult<String> result = bankOrderService.updateRequest(teslaBankRequest);
		return toJson(result);
	}

	@RequestMapping("/bankOrderExport")
	public ResponseEntity<byte[]> bankOrderExport(String selectId , String bankRequestType)
	{
		String fileName ="";
		if(bankRequestType.equals("0")){
			 fileName = "银行订单号查询导出-"+ CommonUtil.getFormatCommonSSS().format(new Date())+".csv";
		}else{
			 fileName = "银行卡号查询导出-"+ CommonUtil.getFormatCommonSSS().format(new Date())+".csv";
		}
		HttpHeaders headers = new HttpHeaders();
		String content  = bankOrderService.exportRequest(selectId , bankRequestType);
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		try {
			headers.setContentDispositionFormData("attachment", new String(fileName.getBytes("utf-8"), "ISO8859-1"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		HttpStatus statusCode = HttpStatus.OK;
		ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(content.getBytes(), headers, statusCode);
		return entity;
	}

	@RequestMapping("/toBatchBankOrderRequestPage")
	public String toBatchBankOrderRequestPage(ModelMap map)
	{
		map.put("bankRequestType","0");
		return "bankorder/batchBankOrderRequestPage";
	}

	@ResponseBody
	@RequestMapping("/batchBankOrderRequest")
	public String batchBankOrderRequest(@RequestParam MultipartFile uploadObject)
	{
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			InputStream inputStream = uploadObject.getInputStream();
			String content = uploadObject.getContentType();
			String orgiginalFileName = uploadObject.getOriginalFilename();
			bankOrderService.batchBankOrderRequest(inputStream ,orgiginalFileName,"0" );
		} catch (IOException e) {
			e.printStackTrace();
		}
		result.put("success",true);
		return toJson(result);
	}

	@ResponseBody
	@RequestMapping("/uploadFile")
	public  String uploadFile(@RequestParam MultipartFile uploadObject)
	{
		return bankOrderService.uploadFile(uploadObject);
	}

	@ResponseBody
	@RequestMapping("/deleteFile")
	public  String deleteFile(String fileName)
	{
		return bankOrderService.deleteFile(fileName);
	}

	@RequestMapping("/downloadFile")
	public ResponseEntity<byte[]> downloadFile(String fileName)
	{
		return bankOrderService.downloadFile(fileName);
	}

}